package com.erp.erp_mask.controller;

import com.erp.erp_mask.mapper.TOrderCommMapper;
import com.erp.erp_mask.mapper.TProductsMapper;
import com.erp.erp_mask.mapper.TSalesordersMapper;
import com.erp.erp_mask.pojo.TProducts;
import com.erp.erp_mask.service.TBomService;
import com.erp.erp_mask.service.TProductsService;
import com.erp.erp_mask.vo.ProOrder;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/duct")
public class TProductsController {
    @Resource
    private TProductsService tProductsService;
    @Resource
    private TProductsMapper tProductsMapper;
    @Resource
    private TBomService tBomService;
    @Resource
    private TSalesordersMapper tSalesordersMapper;
    @Resource
    private TOrderCommMapper tOrderCommMapper;

    @GetMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("/duct/products");
    }

    @GetMapping("/search")
    public Result search() {
        return Result.success(tProductsMapper.searchPro());
    }

    //添加产品
    @PostMapping("/addPro")
    public Result addPro(@RequestBody TProducts tProducts) {
        try {
            tProductsMapper.addProlist(tProducts);
            return Result.success("商品信息添加成功!");
        } catch (Exception e) {
            return Result.fail("商品信息添加失败!");
        }
    }

    //批量删除
    @PutMapping("/prodel")
    public Result prodel(@RequestBody List<TProducts> tProducts) {
        for (TProducts pur : tProducts) {
            tProductsService.del(pur.getProductId());
        }
        return Result.success("成功");
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody TProducts tProducts) {
        try {
            tProductsMapper.editlist(tProducts);
            return Result.success("修改成功!");
        } catch (Exception e) {
            return Result.fail("修改失败!");
        }
    }

    @GetMapping("/cascaderlist")
    public Result cascader() {
        return Result.success(tBomService.cascaderlist());
    }

    @PutMapping("/searchorder")
    public Result searchorder(@RequestBody List<TProducts> tProductsList) {
        if (tProductsList == null || tProductsList.isEmpty()) {
            return Result.success(new ArrayList<>()); // 返回一个空列表
        }
        // 提取所有productId
        List<Integer> productIds = tProductsList.stream()
                .map(TProducts::getProductId)
                .collect(Collectors.toList());
        // 使用IN查询获取对应的TProducts列表
        List<TProducts> resultList = tProductsMapper.ordersearch(productIds);
        return Result.success(resultList);
    }
//添加订单表
    private String number;
    @PostMapping("/orderinsert")
    public Result orderinsert(@RequestBody ProOrder proOrder) {
        try {
            number=proOrder.getOrdernumber();
            tSalesordersMapper.orderinsert(proOrder);
            System.out.println("======99");
            return Result.success("添加成功!");
        } catch (Exception e) {
            return Result.fail("添加失败!");
        }
    }
    //添加订单详细表
    @PostMapping("/ordersinsert")
    public Result ordersinsert(@RequestBody List<TProducts> tProducts) {
        tProducts.stream().forEach(key->{
            key.setOrdernumber(number);
        });
        System.out.println("=====111====="+tProducts);
        try {
            tOrderCommMapper.ordercomminsert(tProducts);
            return Result.success("成功!");
        } catch (Exception e) {
            return Result.fail("失败!");
        }
    }
}
